package com.example.viewpager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

/**
 * @Author：coderChen
 * @Package：com.example.viewpager
 * @Project：ViewPager
 * @Date：2023/12/20 18:46
 * @Filename：ViewPagerActivity
 */
public class ViewPagerActivity extends AppCompatActivity {
    private ArrayList<Fragment> viewList;
    private String titles[]={"首页","设备","我的"};

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        initViews();
    }
    private void initViews(){
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        viewList = new ArrayList<Fragment>();
        viewList.add(new MyFragmen1());
        viewList.add(new MyFragmen2());
        viewList.add(new MyFragmen3());

        MyAdapter adapter = new MyAdapter(getSupportFragmentManager(),getLifecycle(),viewList);
        ViewPager2 viewPager2 = findViewById(R.id.viewPager2);
//        允许左右滑动
        viewPager2.setUserInputEnabled(true);
        viewPager2.setAdapter(adapter);
        new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles[position]);
            }
        }).attach();
    }
}

